var projectnewModule = angular.module('projectnewModule',['Service']);

// 自定义回车事件指令
projectnewModule.directive('ngEnter', function() {
	return function(scope, element, attrs) {
		element.bind("keydown keypress", function(event) {
			if (event.which === 13) {
				scope.$apply(function() {
					scope.$eval(attrs.ngEnter);
				});

				event.preventDefault();
			}
		});
	};
});

// 新建项目控制器
projectnewModule.controller("NewProjectController", function($scope,$http) {
	$scope.new = {
		// 基本信息，第一页
		basicInfo: {
			isActive: true,
			data: {},
			save: function() {
				$scope.new.data = {
					name: $scope.new.basicInfo.name || "",
					projectInfo: $scope.new.basicInfo.intro || ""
				};
				$scope.new.data.tags = $scope.new.tag.tags || "";
				console.log($scope.new.data);
				$scope.new.teamInfo.switchTo();
			},
			switchTo: function() {
				$scope.new.basicInfo.isActive = true;
				$scope.new.teamInfo.isActive = false;
				$scope.new.richInfo.isActive = false;
			}
		},
		// 团队信息，第二页
		teamInfo: {
			isActive: false,
			member: [],
			//member: [{name: $scope.userinfo.name, email: $scope.userinfo.email}],
			//member: [{name: "Andy", email: "tangpin@gmail.com"}],
			// 添加成员
			add: function() {
				$scope.new.teamInfo.member.push({
					name: $scope.new.teamInfo.name,
					email: $scope.new.teamInfo.email
				});
				$scope.new.teamInfo.name = "";
				$scope.new.teamInfo.email = "";
			},
			//保存，切换到下一步
			save: function(){
				$scope.new.data.teamInfo = {
					member: $scope.new.teamInfo.member,
					needTeamate: $scope.new.teamInfo.teamate
				};
				// 将tag信息和团队信息都序列化
				$scope.new.data.tags = JSON.stringify($scope.new.data.tags);
				$scope.new.data.teamInfo = JSON.stringify($scope.new.data.teamInfo);
				console.log($scope.new.data);		
				$http.post('/project',$scope.new.data).success(function(data){
					if(data.success == true){
						console.log(data);	
						$scope.new.richInfo.switchTo();
					}
				});
			},
			switchTo: function() {
				$scope.new.teamInfo.isActive = true;
				$scope.new.richInfo.isActive = false;
				$scope.new.basicInfo.isActive = false;
			}
		},
		// 编辑更多信息，第三页
		// TODO 图片上传问题尚未解决
		richInfo: {
			isActive: false,
			switchTo: function() {
				$scope.new.richInfo.isActive = true;
				$scope.new.teamInfo.isActive = false;
				$scope.new.basicInfo.isActive = false;
			}
		},
		// 标签控制，实际上这部分在基本信息中
		tag: {
			tags: [],
			input: "",
			addTagTitle: "添加标签",
			addTag: function() {
				$scope.new.tag.tags.push($scope.new.tag.input);
				$scope.new.tag.input = "";
			},
			removeTag: function(tag) {
				var arr = $scope.new.tag.tags; 
				for(var i=0; i<arr.length; i++){
					if(arr[i] === tag){
						if(i === 0){
							arr.shift();	
						}else{
							arr.splice(i,i);
						}
					}
					console.log(arr);
				}
			},
			focus: function() {
				$scope.new.tag.addTagTitle = "输入标签并回车";
				$("#addTagInput").focus();
			},
			blur: function() {
				$scope.new.tag.addTagTitle = "添加标签";
			}
		},
		// 最终提交，由最后一个页面调用
		submit: function() {
			$scope.new.data.teamInfo = {
				member: $scope.new.teamInfo.member,
				needTeamate: $scope.new.teamInfo.teamate
			};
            $scope.new.data.tags = $scope.new.tag.tags;
			console.log($scope.new.data);

		}
		
	};
	$('input[id=lefile]').change(function() {
	$('#photoCover').val($(this).val());
	});

$http.get('/user/info').success(function(data){
	if(data.suc == true){
		$scope.userinfo = data;
		console.log($scope.userinfo);
		$scope.new.teamInfo.member.push({
			name:  data.name,
			email: data.email
		});
	}
});
});
